package com.shunda.ysdgj.manager.service.impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.core.util.IdUtil;
import com.shunda.ysdgj.manager.service.CaptchaService;
import com.shunda.ysdgj.model.enums.TimeEnum;
import com.shunda.ysdgj.model.vo.system.CaptchaVo;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@RequiredArgsConstructor
public class CaptchaServiceImpl implements CaptchaService {

    private final RedisTemplate<String, String> redisTemplate;

    @Override
    public CaptchaVo generate() {
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(150, 48, 4, 20);
        String codeValue = captcha.getCode();
        String imageBase64Data = captcha.getImageBase64Data();
        String codeKey = IdUtil.simpleUUID();
        redisTemplate.opsForValue().set(codeKey, codeValue, TimeEnum.ADMIN_CAPTCHA_MINUTES.getTime(), TimeUnit.MINUTES);
        CaptchaVo captchaVo = new CaptchaVo();
        captchaVo.setCodeKey(codeKey);
        captchaVo.setCodeImage(imageBase64Data);
        return captchaVo;
    }

}
